
Skt.RoomModerator = L.Class.extend({
	
	includes: L.Mixin.Events,
	
	events: ['update'],

	initialize: function(room) {
		this._room = room;
		this._activeUsers = [];
		this._editors = [ Number(this._room.getOwnerId()) ];
		this._group = null;
		
		if ( this._room.getType() == Skt.SlideType.GROUP ) {
			this._group = this._room.getRoomList().getGroup();
			var members = this._group.getMembers();
			
			for ( var k = 0; this._group != null && k < members.length; ++k ) {
				if ( !this.isEditor(members[k]) )
					this._editors.push(Number(members[k]));
			}
			
			this._group.on('update', this._handleGroupUpdate, this);
		}
		
		this._couple(this._room.getRoomList().getSketchpad().getOptions().coupling);
	},
	
	_handleGroupUpdate: function( params ) {
		var self = this;
		if ( params.action == 'addMember' )
			self.addEditor(Number(params.userId));
	},
	
	userJoin: function(userId) {
		var self = this;
		if ( !self.isActive(userId) ) {
			self._activeUsers.push(Number(userId));
			
			self.fire('update', {
				action: 'userJoin',
				userId: userId,
				moderator: self
			});
			
			if ( self._group != null )
				self._addEditor(userId);
		}
	},
	
	userLeave: function(userId) {
		var self = this;
		if ( self.isActive(userId) ) {
			self._activeUsers.splice(self._activeUsers.indexOf(Number(userId)), 1);
			self.fire('update', {
				action: 'userLeave',
				userId: userId,
				moderator: self
			});
		}
	},
	
	_addEditor: function(userId) {
		var self = this;
		if ( !self.isEditor(userId) ) {
			self._editors.push(Number(userId));
			self.fire('update', { 
				action: 'addEditor',
				userId: userId,
				moderator: self
			});
		}
	},
	
	addEditor: function(userId) {
		var self = this;
		if ( !self.isEditor(userId) )
			self._addEditor(userId);
	},
	
	removeEditor: function(userId) {
		var self = this;
		if ( self.isEditor(userId) ) {
			self._editors.splice(self._editors.indexOf(Number(userId)), 1);
			
			self.fire('update', { 
				action: 'removeEditor',
				userId: userId,
				moderator: self
			});
		}
	},
	
	requestEditorAddition: function(requesterId, newEditorId) {
		var self = this;
		if ( Number(self._room.getOwnerId()) == Number(requesterId) ) {
			self.addEditor(Number(newEditorId));
			return true;
		} 
		return false;
	},
	
	requestEditorRemoval: function(requesterId, editorId) {
		var self = this;
		if ( Number(self._room.getOwnerId()) == Number(requesterId) ) {
			self.removeEditor(Number(editorId));
			return true;
		}
		return false;
	},
	
	getCurrentlyEditing: function() {
		var self = this;
		res = [];
		
		for ( var i = 0; i < self._editors.length; ++i ) {
			var editor = self._editors[i];
			if ( self.isActive(editor) )
				res.push(editor);
		}			
		
		return res;
	},
	
	canEdit: function(user) {
		var self = this;
		return self.isEditor(Number(user.id)) && user.rol != Skt.UserType.MONITOR; 
	},
	
	isEditor: function(userId) {
		var self = this;
		return $.inArray(Number(userId), self._editors) >= 0;
	},
	
	isActive: function(userId) {
		var self = this;
		return $.inArray(Number(userId), self._activeUsers) >= 0;
	},
	
	getRoom: function() {
		return this._room;
	},
	
	getEditors: function() {
		return this._editors;
	},
	
	getActiveUsers: function() {
		return this._activeUsers;
	},
	
	getState: function() {
		var self = this;
		return {
			activeUsers: self.getActiveUsers(),
			editors: self.getEditors()
		};
	},
	
	setState: function(state) {
		var self = this;
		self._editors = state.editors;
		self._activeUsers = state.activeUsers;
		self.fire('update', { 
			action: 'setState',
			moderator: self
		});
	},

	_couple: function(coupling) {
		if ( coupling.enabled 
				&& this._room.getType() != Skt.SlideType.PERSONAL ) {
			coupling.activityAdapter.coupleObject(
				"roommoderator-" + this._room.getId(),
				this,
				{
					explicitMapping: [ "userJoin", "userLeave", "addEditor", "removeEditor"],
					messageType: "STATE"
				}
			);
		}
	}

});

Skt.RoomModerator.findSelf = function(roomModerator) {
	return Skt.sketchpad.findRoomById(roomModerator._room._id).getRoomModerator();
};
